<?php
/**
 * Copyright (c) 2018-2019.
 *  This file is part of the moonpie production
 *  (c) johnzhang <875010341@qq.com>
 *  This source file is subject to the MIT license that is bundled
 *  with this source code in the file LICENSE.
 */

namespace moonpie\plugin\wechat_account\validate;



use app\common\validate\BaseValidate;
use moonpie\plugin\wechat_account\model\TemplateModel;
use moonpie\plugin\wechat_account\model\WechatAccount;
use moonpie\plugin\wechat_account\service\template\TemplateVarParserInterface;

class WechatTemplateConfigValidator extends BaseValidate
{
    protected $belongAccount;
    protected $exceptId;
    protected $rule = [
        'title' => 'require|length:2,40',
        'account_id' => 'require|integer|validAccountId',
        'description' => 'require|length:2,100',
        'code' => 'require|alphaDash|length:2,20|unique:wechat_template_config',
        'template_parser' => 'validTemplateParser',
        'template_data' => 'validTemplateData',
        'template_id' => 'validTemplateId',
        'status' => 'require|in:0,1',
    ];
    protected $field = [
        'account_id' => '所属公众号',
        'title' => '模板发送器名',
        'description' => '模板发送器描述',
        'code' => '模板发送器机读码',
        'template_parser' => '模板发送器服务ID',
        'template_data' => '模板发送器数据',
        'template_id' => '使用的模板ID',
        'status' => '发送器状态',
    ];
    protected $scene = [
        'create',
        'update' => ['title', 'account_id', 'code' => 'require|validUpdateCode', 'template_parser', 'template_id', 'status'],
    ];
    protected function validUpdateCode($code, $rule, $data, $field, $tilte)
    {
        $label = "{$field}.validUpdateCode";
        if(!$this->is($code, 'alphaDash', $data)){
            $this->message($label, static::$typeMsg['alphaDash']);
            return false;
        }
        if(!$this->length($code, '2,20')) {
            $this->message($label, static::$typeMsg['length']);
            return false;
        }
        if(!$this->unique($code, "wechat_template_config", ['id' => $this->exceptId], 'code')) {
            $this->message($label, static::$typeMsg['unique']);
            return false;
        }
        return true;
    }
    protected function validTemplateParser($parserId, $rule, $data, $field, $title)
    {
        $label = "{$field}.validTemplateParser";
        $container = app(true);
        if(!isset($container[$parserId])){
            $this->message($label, "抱歉，{$title}不是有效的服务数据ID");
            return false;
        }
        if($container[$parserId] instanceof TemplateVarParserInterface) {
            return true;
        }
        $this->message($label, "{$title}必须实现模板变量解析接口.");
        return false;
    }
    protected function validTemplateData($template, $rule, $data, $field, $title)
    {
        return true;
    }
    protected function validTemplateId($templateId, $rule, $data, $field, $title)
    {
        $label = "{$field}.validTemplateId";
        //这里检测模板信息是否匹配
        $account = $this->getBelongAccount($data['account_id']);
        if(!$account) {
            return false;
        }
        $template = new TemplateModel($account);
        $templates = $template->listTemplates();
        if(!$templates) {
            $this->message($label, "抱歉，由于获取本公众号模板消息列表失败，无法获取对应信息");
            return false;
        }
        foreach($templates as $tmpl) {
            if(strcmp($templateId, $tmpl['template_id']) === 0){
                return true;
            }
        }
        $this->message($label, "抱歉，当前{$title}不是有效的模板消息ID");
        return false;
    }
    protected function validAccountId($accountId, $rule, $data, $field, $title)
    {
        $account = $this->getBelongAccount($accountId);
        $label = "{$field}.validAccountId";
        if(!$account) {
            $this->message($label, "{$title}数据信息不存在");
            return false;
        }
        return true;
    }
    protected function getBelongAccount($accountId)
    {
        if(is_null($this->belongAccount) || $this->belongAccount->getId() != $accountId){
            $this->belongAccount = WechatAccount::loadById($accountId);
            if(!$this->belongAccount) {
                $this->belongAccount = null;
            }
        }
        return $this->belongAccount;
    }
    public function useForUpdate($exceptId)
    {
        $this->exceptId = $exceptId;
        return $this->scene('update');
    }
}